﻿using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace aztall.Module.BusinessObjects
{
	[DefaultClassOptions, XafDefaultProperty("DisplayText"), ImageName("BO_Contact"), NavigationItem("Administration")]
	[RuleCombinationOfPropertiesIsUnique("DomainCustomerCode", DefaultContexts.Save, "Domain.Oid,CustomerCode", CustomMessageTemplate = "Customer Code is existed.")]
	public class Customer : DomainObject
	{
		protected string _CustomerCode;
		protected string _CustomerName;
		protected Status _Status;

		public Customer(Session session)
				: base(session)
		{
		}
		public override void AfterConstruction()
		{
			base.AfterConstruction();
		}
		[RuleRequiredField(DefaultContexts.Save, SkipNullOrEmptyValues = false)]
		public string CustomerCode
		{
			get { return _CustomerCode; }
			set { SetPropertyValue(nameof(CustomerCode), ref _CustomerCode, value); }
		}
		public string CustomerName
		{
			get { return _CustomerName; }
			set { SetPropertyValue(nameof(CustomerName), ref _CustomerName, value); }
		}
		[DataSourceCriteria("Category ='" + Status.ActiveInactive + "'")]
		[RuleRequiredField(DefaultContexts.Save, SkipNullOrEmptyValues = false)]
		public Status Status
		{
			get { return _Status; }
			set { SetPropertyValue(nameof(Status), ref _Status, value); }
		}
		[VisibleInListView(false), VisibleInDetailView(false), XafDisplayName("Customer List")]
		public string DisplayText
		{
			get { return string.Format("{0}: {1}", CustomerCode, CustomerName); }
		}

		#region collections
		[Association("PriceList_Customer", UseAssociationNameAsIntermediateTableName = true), DevExpress.Xpo.DisplayName("PriceList"), VisibleInDetailView(true)]
		[DataSourceCriteria("Domain.Oid = '@This.Domain.Oid'")]
		public XPCollection<PriceList> PriceLists
		{
			get
			{
				return GetCollection<PriceList>(nameof(PriceLists));
			}
		}
		[Association("Promo_Customer", UseAssociationNameAsIntermediateTableName = true), DevExpress.Xpo.DisplayName("Promotions")]
		[DataSourceCriteria("Domain.Oid = '@This.Domain.Oid'"), VisibleInDetailView(false)]
		public XPCollection<Promo> Promos
		{
			get { return GetCollection<Promo>(nameof(Promos)); }
		}
		#endregion

	}
}
